
* WHITES
qui{
clear 
use "${data}/HRS_Analysis.dta"
*keep alive and dead: 1 and 4 are alive and 5 and 6 are dead
keep if iwstatr==1 | iwstatr==4 

* keep Whites
drop if hispanic==1 // drop Hispanics
drop if race==2 // drop Blacks

rename iwendmr month // interview end date month

gen educ_group=1 if raedyrs <=12
replace educ_group=2 if raedyrs>=13 & raedyrs<16
replace educ_group=3 if raedyrs>=16 & raedyrs!=.

keep if gender==0  // keep males 0=male, 1=women

gen MAR=1 if  marital_status==1
replace MAR=0 if marital_status==2 | marital_status==3

keep marital_status MAR wave ID hhid pn gender age dyears year wtcrnhr educ_group

* dyears is the year of death of the spouse

sort ID year
gen MAR_transition = 0 if MAR==1 & ID==ID[_n+1]  & ( MAR[_n+1]==1 |   (MAR[_n+1]==0 & (dyears!=year & dyears!=year+1) ) ) // transition=0 if stil married next period (2 yrs from now) or wife died this year or next year
replace MAR_transition = 1 if MAR==1 & ID==ID[_n+1]  & MAR[_n+1]==0 & (dyears==year | dyears==year+1) //wife died this year or next  
drop if MAR_transition==.
tab MAR_transition

drop if age<60
drop if age>=100
**** calculate probability of becoming single 

fillin age educ_group
gen agesq=age^2

logit MAR_transition age agesq  i.educ_group [pweight=wtcrnhr]
	predict Y, pr
	collapse Y, by(age educ_group)
	
drop if age<65

* graph 
#delimit ;
line Y age if educ_group==1 , yaxis(1) xaxis(1) lwidth(medthick) lcolor(edkblue) 
|| line Y age if educ_group==2 , lwidth(medthick)  lcolor(cranberry)
|| line Y age if educ_group==3 , lwidth(medthick) lcolor(edkblue)   lpattern(dash)
  ||, 
  ylabel(0.0(.05).20)
  xlabel(65(5)100)
  ytitle("Probability")
xtitle("Age")
   title("Probability of Transitioning from Married to Widower", color(black))
      subtitle("by Education")
   legend(label(1 "HS or Less")  label(2 "Some College") label(3 "College")  )
  legend(col(3) pos(6) region(lcolor(gs16)))
plotregion(margin(r+7 l+5) style(none))
  graphregion(icolor(white) fcolor(gs14) margin(none ))
  graphregion(color(white)) bgcolor(white)
  ;
#delimit cr
graph export "${out_figures}\Marriage_rates_65.eps", replace

sort educ_group age
drop educ_group age
outsheet using "${data_model}/Marriage3.txt", nolabel nonames replace	
}

*** RACIAL MINORITIES ***

clear 
use "${data}/HRS_Analysis.dta"
*keep alive and dead: 1 and 4 are alive and 5 and 6 are dead
keep if iwstatr==1 | iwstatr==4 

gen race_use = 1 if hispanic!=1 & race!=2
replace race_use = 3 if hispanic==1 // hispanic
replace race_use =2 if race==2 // black

rename iwendmr month // interview end date month


gen educ_group=1 if raedyrs <=12
replace educ_group=2 if raedyrs>=13 & raedyrs<16
replace educ_group=3 if raedyrs>=16 & raedyrs!=.
keep if educ_group==1
keep if gender==0  // keep males 0=male, 1=women

gen MAR=1 if  marital_status==1
replace MAR=0 if marital_status==2 | marital_status==3

keep marital_status MAR wave ID hhid pn gender age dyears year wtcrnhr race_use

* dyears is the year of death of the spouse

sort ID year
gen MAR_transition = 0 if MAR==1 & ID==ID[_n+1]  & ( MAR[_n+1]==1 |   (MAR[_n+1]==0 & (dyears!=year & dyears!=year+1) ) ) // transition=0 if stil married next period (2 yrs from now) or wife died this year or next year
replace MAR_transition = 1 if MAR==1 & ID==ID[_n+1]  & MAR[_n+1]==0 & (dyears==year | dyears==year+1) //wife died this year or next  
drop if MAR_transition==.
tab MAR_transition

drop if age<60
drop if age>=100
**** calculate probability of becoming single 

insobs 1
replace age=98 if age==.
fillin age  race_use
gen agesq=age^2



logit MAR_transition age agesq  i.race_use [pweight=wtcrnhr]
	predict Y, pr
	collapse Y, by(age race_use)
	
drop if age<65

* graph 
#delimit ;
line Y age if race_use==1 , yaxis(1) xaxis(1) lwidth(medthick) lcolor(edkblue) 
|| line Y age if race_use==2 , lwidth(medthick)  lcolor(cranberry)
|| line Y age if race_use==3 , lwidth(medthick) lcolor(edkblue)   lpattern(dash)
  ||, 
  ylabel(0.0(.05).20)
  xlabel(65(5)100)
  ytitle("Probability")
xtitle("Age")
   title("Probability of Transitioning from Married to Widower", color(black))
      subtitle("by Race")
   legend(label(1 "White")  label(2 "Black") label(3 "Hispanic")  )
  legend(col(3) pos(6) region(lcolor(gs16)))
plotregion(margin(r+7 l+5) style(none))
  graphregion(icolor(white) fcolor(gs14) margin(none ))
  graphregion(color(white)) bgcolor(white)
  ;
#delimit cr
graph export "${out_figures}\Marriage_rates_65_races.eps", replace

drop if race_use==1

preserve
keep if race_use==2 // blacks
sort age
drop age race_use
outsheet using "${data_model}/Blacks\Marriage3.txt", nolabel nonames replace	
restore 

keep if race_use==3 // hisp
sort age
drop age race_use
outsheet using "${data_model}/Hispanics\Marriage3.txt", nolabel nonames replace